package components.gamelevels;

import java.util.ArrayList;
import java.util.List;

import javax.management.RuntimeErrorException;

import components.blocks.Block;

public class BlocksMatrix {

	private int xStart;
	private int yStart;
	private int xEnd;
	private int yEnd;
	private int separation;
	private int blockWidth;
	private int blockHeight;
	
	private List<Block> blocks;
	
	public BlocksMatrix(int xs, int ys, int xe, int ye, int aSeparation, int bWidth, int bHeight){
		this.blocks = new ArrayList<Block>();
		this.xStart = xs;
		this.xEnd = xe;
		this.yStart = ys;
		this.yEnd = ye;
		this.separation = aSeparation;
		this.setBlockHeight(bHeight);
		this.setBlockWidth(bWidth);
	}
	public BlocksMatrix(int aSeparation, int bWidth, int bHeight){
		this.blocks = new ArrayList<Block>();
		this.xStart = 50;
		this.xEnd = 700;
		this.yStart = 50;
		this.yEnd = 400;
		this.separation = aSeparation;
		this.setBlockHeight(bHeight);
		this.setBlockWidth(bWidth);
	}


	public void addBlock(Block block, int x, int y){
		this.getBlocks().add(block);
		block.setX(this.getXfor(x));
		block.setY(this.getYfor(y));
	}

	public int getXfor(int x){
		int res = this.getxStart() + x *(this.getBlockWidth() + this.getSeparation());
		if (res + this.getBlockWidth() > this.getxEnd() | res < this.getxStart()){
			throw new RuntimeException("Added block is out of bounds");
		}
		return res;
	}
	public int getYfor(int y){
		int res = this.getyStart() + y *(this.getBlockHeight() + this.getSeparation());
		if (res + this.getBlockHeight() > this.getyEnd() | res < this.getyStart()){
			throw new RuntimeException("Added block is out of bounds");
		}
		return res;
	}
	
	public int getWidth(){
		return (this.getxEnd() - this.getxStart())/this.getSeparation();
	}
	public int getHeight(){
		return (this.getyEnd() - this.getyStart())/this.getSeparation();
	}
	
	public int getxStart() {
		return xStart;
	}

	public void setxStart(int xStart) {
		this.xStart = xStart;
	}

	public int getyStart() {
		return yStart;
	}

	public void setyStart(int yStart) {
		this.yStart = yStart;
	}

	public int getxEnd() {
		return xEnd;
	}

	public void setxEnd(int xEnd) {
		this.xEnd = xEnd;
	}

	public int getyEnd() {
		return yEnd;
	}

	public void setyEnd(int yEnd) {
		this.yEnd = yEnd;
	}

	public int getSeparation() {
		return separation;
	}

	public void setSeparation(int separation) {
		this.separation = separation;
	}

	public List<Block> getBlocks() {
		return blocks;
	}


	public void setBlockWidth(int blockWidth) {
		this.blockWidth = blockWidth;
	}


	public int getBlockWidth() {
		return blockWidth;
	}


	public void setBlockHeight(int blockHeight) {
		this.blockHeight = blockHeight;
	}


	public int getBlockHeight() {
		return blockHeight;
	}
}
